﻿using Microsoft.Extensions.Configuration;

namespace Luka;

/// <summary>
/// 全局信息(仅读)
/// </summary>
[NotScanned]
public abstract class App
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public static TUser? GetUser<TUser>() where TUser : class, new() => InternalApp.User as TUser;

    /// <summary>
    /// 用户信息
    /// </summary>
    /// <returns></returns>
    public static dynamic? GetUser()
    {
        if (InternalApp.UserType != null)
        {
            return Convert.ChangeType(InternalApp.User, InternalApp.UserType);
        }
        return InternalApp.User;
    }

    /// <summary>
    /// 存储根服务
    /// </summary>
    public static IServiceProvider? RootServices => InternalApp.RootServices;

    /// <summary>
    /// 配置项
    /// </summary>
    public static IConfiguration? RootConfiguration => InternalApp.RootConfiguration;

    /// <summary>
    /// 根据dll获取所有类型
    /// </summary>
    /// <returns></returns>
    public static IEnumerable<Type> TypeList => InternalApp.TypeList;

    /// <summary>
    /// 根据dll获取类型
    /// </summary>
    /// <typeparam name="Ttype">类型</typeparam>
    /// <returns></returns>
    public static Type? GetType<Ttype>() where Ttype : class => InternalApp.GetType<Ttype>();

    /// <summary>
    ///  获取服务
    /// </summary>
    /// <typeparam name="TService">服务类型</typeparam>
    /// <returns></returns>
    public static TService? GetService<TService>() where TService : class => InternalApp.GetService<TService>();

    /// <summary>
    /// 判断配置是否存在
    /// </summary>
    /// <param name="key">节点路径</param>
    /// <returns>是否存在</returns>
    public static bool Exists(string key) => InternalApp.Exists(key);

    /// <summary>
    /// 获取appsetting文件信息
    /// </summary>
    /// <param name="key">键名</param>
    /// <returns></returns>
    public static string GetSection(string key) => InternalApp.GetSection(key);

    /// <summary>
    /// 获取appsetting文件信息
    /// </summary>
    /// <typeparam name="TOptions">泛型类型</typeparam>
    /// <param name="key">键名</param>
    /// <returns></returns>
    public static TOptions GetSection<TOptions>(string key) where TOptions : class, new() => InternalApp.GetSection<TOptions>(key);
}